﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading;
using System.Threading.Tasks;

using Meilisearch;

namespace HDLogCenter.Infrastructure.ExternalServices
{
    public class MeiliIndexHelper
    {
        private readonly HttpClient _http;
        private readonly string _indexUid;

        public MeiliIndexHelper(HttpClient httpClient, string indexUid)
        {
            _http = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
            _indexUid = indexUid ?? throw new ArgumentNullException(nameof(indexUid));
        }

        /// <summary>
        /// 更新 filterable attributes
        /// </summary>
        //public async Task<TaskInfo> UpdateFilterableAttributesAsync(IEnumerable<string> filterableAttributes, CancellationToken cancellationToken = default)
        //{
        //    if (filterableAttributes == null) throw new ArgumentNullException(nameof(filterableAttributes));

        //    var response = await _http.PostAsJsonAsync(
        //        $"indexes/{_indexUid}/settings/filterable-attributes",
        //        filterableAttributes,
        //        cancellationToken
        //    );

        //    response.EnsureSuccessStatusCode();
        //    var task = await response.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken);
        //    return task;
        //}

        /// <summary>
        /// 更新 sortable attributes
        /// </summary>
        //public async Task<TaskInfo> UpdateSortableAttributesAsync(IEnumerable<string> sortableAttributes, CancellationToken cancellationToken = default)
        //{
        //    if (sortableAttributes == null) throw new ArgumentNullException(nameof(sortableAttributes));

        //    var response = await _http.PostAsJsonAsync(
        //        $"indexes/{_indexUid}/settings/sortable-attributes",
        //        sortableAttributes,
        //        cancellationToken
        //    );

        //    response.EnsureSuccessStatusCode();
        //    var task = await response.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken);
        //    return task;
        //}

        /// <summary>
        /// 查询任务状态
        /// </summary>
        //public async Task<TaskInfo> GetTaskAsync(int taskId, CancellationToken cancellationToken = default)
        //{
        //    var response = await _http.GetAsync(
        //        $"tasks/{taskId}",
        //        cancellationToken
        //    );

        //    response.EnsureSuccessStatusCode();
        //    var task = await response.Content.ReadFromJsonAsync<TaskInfo>(cancellationToken: cancellationToken);
        //    return task;
        //}
    }
}
